package com.wjy.common.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wjy.common.config.jackson.JacksonConfig;

/**
 * @author ybxxszl
 * @desc Jackson工具类
 * @data 2019年4月12日
 */
public class JacksonUtil {

    private static JacksonConfig config = new JacksonConfig();
    private static ObjectMapper mapper = new ObjectMapper();

    public static ObjectMapper getObjectMapper() {
        return config.objectMapper(mapper);
    }

    /**
     * @param content
     * @param valueType
     * @return
     * @desc String转换为Entity
     */
    public static <T> T toEntity(String content, Class<T> valueType) {
        try {
            return getObjectMapper().readValue(content, valueType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param object
     * @return
     * @desc Object转换为String
     */
    public static String toString(Object object) {
        try {
            return getObjectMapper().writeValueAsString(object);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

}
